package com.dpiinc.TechPage.oss

import android.util.Log
import com.alibaba.sdk.android.oss.ClientException
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken
import com.dpiinc.TechPage.AIWriteSettings
import com.dpiinc.TechPage.AWLog
import com.dpiinc.TechPage.UserManager
import com.dpiinc.TechPage.api.RetrofitFactory

/**
 * Created by jingdan on 2017/11/15.
 * Authentication server issued under the agreement of the official website agreement, you can directly use the provider
 */

@Suppress("unused")
class AiWriteAuthCredentialsProvider : OSSFederationCredentialProvider() {

    private var mDecoder: AuthDecoder? = null

    /**
     * set response data decoder
     *
     * @param decoder
     */
    fun setDecoder(decoder: AuthDecoder) {
        this.mDecoder = decoder
    }

    @Throws(ClientException::class)
    override fun getFederationToken(): OSSFederationToken {
        var authToken = OSSFederationToken("", "", "", 0)

        val params = HashMap<String, String>()
        params["uId"] = UserManager.getUserId()
        params["accessToken"] = UserManager.getAccessToken()

        val request = RetrofitFactory.getInstance().getOssAuthCredentials(params)
        request.subscribe({
            AWLog.i("OssAuthCredentials", it.toString())

            //OssAuthCredentials(AccessKeyId=STS.NTGDLwfmU9n1WQyHU6vr92kat, OssPath=OssPath(bucketName=bjaiwrite2,
            // recordPath=aiwrite/app/1084137/record, imagePath=aiwrite/app/1084137/dotPicture, dotPath=aiwrite/app/1084137/dotData),

            // StatusCode=0, AccessKeySecret=FXuidGXSpFUxRjfr5Y3eCWGgdbjJv4GUYnkMjbKRHgY7,
            // SecurityToken=CAIS8QF1q6Ft5B2yfSjIr5fyD/bDi7J0jqzaVXfIrFVjev0V3a7Kljz2IHhJfnNtAOsas/o+n21Q7PcdlqBpQppCXlfIYNUoQTqID8/5MeT7oMWQweEuqv
            // /MQBq+aXPS2MvVfJ+KLrf0ceusbFbpjzJ6xaCAGxypQ12iN+/i6/clFKN1ODO1dj1bHtxbCxJ/ocsBTxvrOO2qLwThjxi7b
            // iMqmHIl0T0nuPTvmJHMtkOD0wKn8IJP+dSteKrDRtJ3IZJyX+2y2OFLbafb2EZSkUMSqf0s0fUfpWqZ74rAWwMAvA/nKePR9cZ0IAJndl
            // /u0lvFWmGVGoABph6/65M5toElRuTL9fzwstB0vC292KOdjKXZMuDuRXwuwFgmHMmOujKR1dCbbsjb6X4tyeloA8o1aWJxAA2FZSU+R+Dq3V44Fpz
            // 46tf+4GnoQ9T+TvH0E7X+ZtOh1saMi7a/ZHykZMtd16kYOavcF4oU07qGv8SEros6JSjExsM=,
            // Expiration=2022-07-28T03:16:05Z, ErrorCode=null, ErrorMessage=null)
            if (it.StatusCode == 0) {
                authToken = OSSFederationToken(it.AccessKeyId, it.AccessKeySecret, it.SecurityToken, it.Expiration)
                val ossPath = it.OssPath
                AIWriteSettings.setOssBucket(ossPath.bucketName)
                AIWriteSettings.setOssAudioFilePrefix(ossPath.recordPath)
                AIWriteSettings.setOssDotFilePrefix(ossPath.dotPath)
                AIWriteSettings.setOssImgFilePrefix(ossPath.imagePath)
            } else {
                throw ClientException("ErrorCode: ${it.ErrorCode}| ErrorMessage: ${it.ErrorMessage}")
            }
        }, {
            throw ClientException(it)
        })
        return authToken
    }

    interface AuthDecoder {
        fun decode(data: String): String
    }
}
